<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- /fasttmp/mkdist-qt-4.3.5-1211793125/qtopia-core-opensource-src-4.3.5/doc/src/paintsystem.qdoc -->
<head>
  <title>Qt 4.3: The Paint System</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">The Paint System<br /><small></small></h1>
<p>Qt's paint system enables painting on screen and print devices using the same API, and is primarily based on the <a href="qpainter.html">QPainter</a>, <a href="qpaintdevice.html">QPaintDevice</a>, and <a href="qpaintengine.html">QPaintEngine</a> classes.</p>
<p><a href="qpainter.html">QPainter</a> is used to perform drawing operations, <a href="qpaintdevice.html">QPaintDevice</a> is an abstraction of a two-dimensional space that can be painted on using a <a href="qpainter.html">QPainter</a>, and <a href="qpaintengine.html">QPaintEngine</a> provides the interface that the painter uses to draw onto different types of devices. The <a href="qpaintengine.html">QPaintEngine</a> class is used internally by <a href="qpainter.html">QPainter</a> and <a href="qpaintdevice.html">QPaintDevice</a>, and is hidden from application programmers unless they create their own device type.</p>
<p align="center"><img src="images/paintsystem-core.png" /></p><p>The main benefit of this approach is that all painting follows the same painting pipeline making it easy to add support for new features and providing default implementations for unsupported ones.</p>
<p>Alternatively, Qt provides the <a href="qtopengl.html">QtOpenGL</a> module, offering classes that makes it easy to use OpenGL in Qt applications. Among others, the module provides an OpenGL widget class that can be used just like any other Qt widget, except that it opens an OpenGL display buffer where the OpenGL API can be used to render the contents.</p>
<ul><li><a href="#drawing">Drawing</a></li>
<li><a href="#filling">Filling</a></li>
<li><a href="#creating-a-paint-device">Creating a Paint Device</a></li>
<li><a href="#reading-and-writing-image-files">Reading and Writing Image Files</a></li>
<li><a href="#styling">Styling</a></li>
</ul>
<a name="drawing"></a>
<h2>Drawing</h2>
<p><a href="qpainter.html">QPainter</a> provides highly optimized functions to do most of the drawing GUI programs require. It can draw everything from simple graphical primitives (represented by the <a href="qpoint.html">QPoint</a>, <a href="qline.html">QLine</a>, <a href="qrect.html">QRect</a>, <a href="qregion.html">QRegion</a> and <a href="qpolygon.html">QPolygon</a> classes) to complex shapes like vector paths. In Qt vector paths are represented by the <a href="qpainterpath.html">QPainterPath</a> class. <a href="qpainterpath.html">QPainterPath</a> provides a container for painting operations, enabling graphical shapes to be constructed and reused.</p>
<p><table width="100%" align="center" cellpadding="2" cellspacing="1" border="0">
<tr valign="top" class="odd"><td><p align="center"><img src="images/paintsystem-painterpath.png" /></p></td><td><b>QPainterPath</b><p>A painter path is an object composed of lines and curves. For example, a rectangle is composed by lines and an ellipse is composed by curves.</p>
<p>The main advantage of painter paths over normal drawing operations is that complex shapes only need to be created once; then they can be drawn many times using only calls to the <a href="qpainter.html#drawPath">QPainter::drawPath</a>() function.</p>
<p>A <a href="qpainterpath.html">QPainterPath</a> object can be used for filling, outlining, and clipping. To generate fillable outlines for a given painter path, use the <a href="qpainterpathstroker.html">QPainterPathStroker</a> class.</p>
</td></tr>
</table></p>
<p>Lines and outlines are drawn using the <a href="qpen.html">QPen</a> class. A pen is defined by its style (i.e&#x2e; its line-type), width, brush, how the endpoints are drawn (cap-style) and how joins between two connected lines are drawn (join-style). The pen's brush is a <a href="qbrush.html">QBrush</a> object used to fill strokes generated with the pen, i.e&#x2e; the <a href="qbrush.html">QBrush</a> class defines the fill pattern.</p>
<p><a href="qpainter.html">QPainter</a> can also draw aligned text and pixmaps.</p>
<p>When drawing text, the font is specified using the <a href="qfont.html">QFont</a> class. Qt will use the font with the specified attributes, or if no matching font exists, Qt will use the closest matching installed font. The attributes of the font that is actually used can be retrieved using the <a href="qfontinfo.html">QFontInfo</a> class. In addition, the <a href="qfontmetrics.html">QFontMetrics</a> class provides the font measurements, and the <a href="qfontdatabase.html">QFontDatabase</a> class provides information about the fonts available in the underlying window system.</p>
<p>Normally, <a href="qpainter.html">QPainter</a> draws in a &quot;natural&quot; coordinate system, but it is able to perform view and world transformations using the <a href="qmatrix.html">QMatrix</a> class. For more information, see <a href="coordsys.html">The Coordinate System</a> documentation which also describes the rendering process, i.e&#x2e; the relation between the logical representation and the rendered pixels, and the benefits of anti-aliased painting.</p>
<p><table width="100%" align="center" cellpadding="2" cellspacing="1" border="0">
<tr valign="top" class="odd"><td><b>Anti-Aliased Painting</b><p>When drawing, the pixel rendering is controlled by the <a href="qpainter.html#RenderHint-enum">QPainter::Antialiasing</a> render hint. The <a href="qpainter.html#RenderHint-enum">QPainter::RenderHint</a> enum is used to specify flags to <a href="qpainter.html">QPainter</a> that may or may not be respected by any given engine.</p>
<p>The <a href="qpainter.html#RenderHint-enum">QPainter::Antialiasing</a> value indicates that the engine should antialias edges of primitives if possible, i.e&#x2e; smoothing the edges by using different color intensities.</p>
</td><td><p align="center"><img src="images/paintsystem-antialiasing.png" /></p></td></tr>
</table></p>
<a name="filling"></a>
<h2>Filling</h2>
<p>Shapes are filled using the <a href="qbrush.html">QBrush</a> class. A brush is defined by its color and its style (i.e&#x2e; its fill pattern).</p>
<p>Any color in Qt is represented by the <a href="qcolor.html">QColor</a> class which supports the RGB, HSV and CMYK color models. <a href="qcolor.html">QColor</a> also support alpha-blended outlining and filling (specifying the transparency effect), and the class is platform and device independent (the colors are mapped to hardware using the <a href="qcolormap.html">QColormap</a> class). For more information, see the <a href="qcolor.html">QColor</a> class documentation.</p>
<p>When creating a new widget, it is recommend to use the colors in the widget's palette rather than hard-coding specific colors. All widgets in Qt contain a palette and use their palette to draw themselves. A widget's palette is represented by the <a href="qpalette.html">QPalette</a> class which contains color groups for each widget state.</p>
<p>The available fill patterns are described by the <a href="qt.html#BrushStyle-enum">Qt::BrushStyle</a> enum. These include basic patterns spanning from uniform color to very sparse pattern, various line combinations, gradient fills and textures. Qt provides the <a href="qgradient.html">QGradient</a> class to define custom gradient fills, while texture patterns are specified using the <a href="qpixmap.html">QPixmap</a> class.</p>
<p><table width="100%" align="center" cellpadding="2" cellspacing="1" border="0">
<tr valign="top" class="odd"><td><p align="center"><img src="images/paintsystem-fancygradient.png" /></p></td><td><b>QGradient</b><p>The <a href="qgradient.html">QGradient</a> class is used in combination with <a href="qbrush.html">QBrush</a> to specify gradient fills.</p>
<p align="center"><img src="images/paintsystem-gradients.png" /></p><p>Qt currently supports three types of gradient fills: Linear gradients interpolate colors between start and end points, radial gradients interpolate colors between a focal point and end points on a circle surrounding it, and conical gradients interpolate colors around a center point.</p>
</td></tr>
</table></p>
<a name="creating-a-paint-device"></a>
<h2>Creating a Paint Device</h2>
<p>The <a href="qpaintdevice.html">QPaintDevice</a> class is the base class of objects that can be painted, i.e&#x2e; <a href="qpainter.html">QPainter</a> can draw on any <a href="qpaintdevice.html">QPaintDevice</a> subclass. <a href="qpaintdevice.html">QPaintDevice</a>'s drawing capabilities are currently implemented by the <a href="qwidget.html">QWidget</a>, <a href="qimage.html">QImage</a>, <a href="qpixmap.html">QPixmap</a>, <a href="qglwidget.html">QGLWidget</a>, <a href="qglpixelbuffer.html">QGLPixelBuffer</a>, <a href="qpicture.html">QPicture</a> and <a href="qprinter.html">QPrinter</a> subclasses.</p>
<p align="center"><img src="images/paintsystem-devices.png" /></p><p><table width="100%" align="center" cellpadding="2" cellspacing="1" border="0">
<tr valign="top" class="odd"><td><b>Custom Backends</b><p>Support for a new backend can be implemented by deriving from the <a href="qpaintdevice.html">QPaintDevice</a> class and reimplementing the virtual <a href="qpaintdevice.html#paintEngine">QPaintDevice::paintEngine</a>() function to tell <a href="qpainter.html">QPainter</a> which paint engine should be used to draw on this particular device. To actually be able to draw on the device, this paint engine must be a custom paint engine created by deriving from the <a href="qpaintengine.html">QPaintEngine</a> class.</p>
</td></tr>
</table></p>
<a name="widget"></a>
<h3>Widget</h3>
<p>The <a href="qwidget.html">QWidget</a> class is the base class of all user interface objects. The widget is the atom of the user interface: it receives mouse, keyboard and other events from the window system, and paints a representation of itself on the screen.</p>
<a name="image"></a>
<h3>Image</h3>
<p>The <a href="qimage.html">QImage</a> class provides a hardware-independent image representation which is designed and optimized for I/O, and for direct pixel access and manipulation. <a href="qimage.html">QImage</a> supports several image formats including monochrome, 8-bit, 32-bit and alpha-blended images.</p>
<p>One advantage of using <a href="qimage.html">QImage</a> as a paint device is that it is possible to guarantee the pixel exactness of any drawing operation in a platform-independent way. Another benefit is that the painting can be performed in another thread than the current GUI thread.</p>
<a name="pixmap"></a>
<h3>Pixmap</h3>
<p>The <a href="qpixmap.html">QPixmap</a> class is an off-screen image representation which is designed and optimized for showing images on screen. Unlike <a href="qimage.html">QImage</a>, the pixel data in a pixmap is internal and is managed by the underlying window system, i.e&#x2e; pixels can only be accessed through <a href="qpainter.html">QPainter</a> functions or by converting the <a href="qpixmap.html">QPixmap</a> to a <a href="qimage.html">QImage</a>.</p>
<p>To optimize drawing with <a href="qpixmap.html">QPixmap</a>, Qt provides the <a href="qpixmapcache.html">QPixmapCache</a> class which can be used to store temporary pixmaps that are expensive to generate without using more storage space than the cache limit.</p>
<p>Qt also provides the <a href="qbitmap.html">QBitmap</a> convenience class, inheriting <a href="qpixmap.html">QPixmap</a>. <a href="qbitmap.html">QBitmap</a> guarantees monochrome (1-bit depth) pixmaps, and is mainly used for creating custom <a href="qcursor.html">QCursor</a> and <a href="qbrush.html">QBrush</a> objects, constructing <a href="qregion.html">QRegion</a> objects, and for setting masks for pixmaps and widgets.</p>
<a name="opengl-widget"></a>
<h3>OpenGL Widget</h3>
<p>As mentioned above, Qt provides the <a href="qtopengl.html">QtOpenGL</a> module offering classes that makes it easy to use OpenGL in Qt applications. For example, the <a href="qglwidget.html">QGLWidget</a> enables the OpenGL API for rendering.</p>
<p>But <a href="qglwidget.html">QGLWidget</a> is also a <a href="qwidget.html">QWidget</a> subclass, and can be used by <a href="qpainter.html">QPainter</a> as any other paint device. One huge benefit from this is that it enables Qt to utilize the high performance of OpenGL for most drawing operations, such as transformations and pixmap drawing.</p>
<a name="pixel-buffer"></a>
<h3>Pixel Buffer</h3>
<p>The <a href="qtopengl.html">QtOpenGL</a> module also provides the <a href="qglpixelbuffer.html">QGLPixelBuffer</a> class which inherits <a href="qpaintdevice.html">QPaintDevice</a> directly.</p>
<p><a href="qglpixelbuffer.html">QGLPixelBuffer</a> encapsulates an OpenGL pbuffer. Rendering into a pbuffer is normally done using full hardware acceleration which can be significantly faster than rendering into a <a href="qpixmap.html">QPixmap</a>.</p>
<a name="framebuffer-object"></a>
<h3>Framebuffer Object</h3>
<p>The <a href="qtopengl.html">QtOpenGL</a> module also provides the <a href="qglframebufferobject.html">QGLFramebufferObject</a> class which inherits <a href="qpaintdevice.html">QPaintDevice</a> directly.</p>
<p><a href="qglframebufferobject.html">QGLFramebufferObject</a> encapsulates an OpenGL framebuffer object. Framebuffer objects can also be used for off-screen rendering, and offer several advantages over pixel buffers for this purpose. These are described in the <a href="qglframebufferobject.html">QGLFramebufferObject</a> class documentation.</p>
<a name="picture"></a>
<h3>Picture</h3>
<p>The <a href="qpicture.html">QPicture</a> class is a paint device that records and replays <a href="qpainter.html">QPainter</a> commands. A picture serializes painter commands to an IO device in a platform-independent format. <a href="qpicture.html">QPicture</a> is also resolution independent, i.e&#x2e; a <a href="qpicture.html">QPicture</a> can be displayed on different devices (for example svg, pdf, ps, printer and screen) looking the same.</p>
<p>Qt provides the <a href="qpicture.html#load">QPicture::load</a>() and <a href="qpicture.html#save">QPicture::save</a>() functions for loading and saving pictures. But in addition the <a href="qpictureio.html">QPictureIO</a> class is provided to enable the programmer to install new picture file formats in addition to those that Qt provides.</p>
<a name="printer"></a>
<h3>Printer</h3>
<p>The <a href="qprinter.html">QPrinter</a> class is a paint device that paints on a printer. On Windows or Mac OS X, <a href="qprinter.html">QPrinter</a> uses the built-in printer drivers. On X11, <a href="qprinter.html">QPrinter</a> generates postscript and sends that to lpr, lp, or another print program. <a href="qprinter.html">QPrinter</a> can also print to any other <a href="qprintengine.html">QPrintEngine</a> object.</p>
<p>The <a href="qprintengine.html">QPrintEngine</a> class defines an interface for how <a href="qprinter.html">QPrinter</a> interacts with a given printing subsystem. The common case when creating your own print engine, is to derive from both <a href="qpaintengine.html">QPaintEngine</a> and <a href="qprintengine.html">QPrintEngine</a>.</p>
<p>The output format is by default determined by the platform the printer is running on, but by explicitly setting the output format to <a href="qprinter.html#OutputFormat-enum">QPrinter::PdfFormat</a>, <a href="qprinter.html">QPrinter</a> will generate its output as a PDF file.</p>
<a name="reading-and-writing-image-files"></a>
<h2>Reading and Writing Image Files</h2>
<p>The most common way to read images is through <a href="qimage.html">QImage</a> and <a href="qpixmap.html">QPixmap</a>'s constructors, or by calling the <a href="qimage.html#load">QImage::load</a>() and <a href="qpixmap.html#load">QPixmap::load</a>() functions. In addition, Qt provides the <a href="qimagereader.html">QImageReader</a> class which gives more control over the process. Depending on the underlying support in the image format, the functions provided by the class can save memory and speed up loading of images.</p>
<p>Likewise, Qt provides the <a href="qimagewriter.html">QImageWriter</a> class which supports setting format specific options, such as the gamma level, compression level and quality, prior to storing the image. If you do not need such options, you can use <a href="qimage.html#save">QImage::save</a>() or <a href="qpixmap.html#save">QPixmap::save</a>() instead.</p>
<p><table width="100%" align="center" cellpadding="2" cellspacing="1" border="0">
<tr valign="top" class="odd"><td><b>QMovie</b><p><a href="qmovie.html">QMovie</a> is a convenience class for displaying animations, using the <a href="qimagereader.html">QImageReader</a> class internally. Once created, the <a href="qmovie.html">QMovie</a> class provides various functions for both running and controlling the given animation.</p>
</td><td><p align="center"><img src="images/paintsystem-movie.png" /></p></td></tr>
</table></p>
<p>The <a href="qimagereader.html">QImageReader</a> and <a href="qimagewriter.html">QImageWriter</a> classes rely on the <a href="qimageiohandler.html">QImageIOHandler</a> class which is the common image I/O interface for all image formats in Qt. <a href="qimageiohandler.html">QImageIOHandler</a> objects are used internally by <a href="qimagereader.html">QImageReader</a> and <a href="qimagewriter.html">QImageWriter</a> to add support for different image formats to Qt.</p>
<p>A list of the supported file formats are available through the <a href="qimagereader.html#supportedImageFormats">QImageReader::supportedImageFormats</a>() and <a href="qimagewriter.html#supportedImageFormats">QImageWriter::supportedImageFormats</a>() functions. Qt supports several file formats by default, and in addition new formats can be added as plugins. The currently supported formats are listed in the <a href="qimagereader.html">QImageReader</a> and <a href="qimagewriter.html">QImageWriter</a> class documentation.</p>
<p>Qt's plugin mechanism can also be used to write a custom image format handler. This is done by deriving from the <a href="qimageiohandler.html">QImageIOHandler</a> class, and creating a <a href="qimageioplugin.html">QImageIOPlugin</a> object which is a factory for creating <a href="qimageiohandler.html">QImageIOHandler</a> objects. When the plugin is installed, <a href="qimagereader.html">QImageReader</a> and <a href="qimagewriter.html">QImageWriter</a> will automatically load the plugin and start using it.</p>
<p><table width="100%" align="center" cellpadding="2" cellspacing="1" border="0">
<tr valign="top" class="odd"><td><p align="center"><img src="images/paintsystem-svg.png" /></p></td><td><b>SVG Rendering</b><p>Scalable Vector Graphics (SVG) is an language for describing both static and animated two-dimensional vector graphics. Qt includes support for the static features of SVG 1.2 Tiny.</p>
<p>SVG drawings can be rendered onto any <a href="qpaintdevice.html">QPaintDevice</a> subclass. This approach gives developers the flexibility to experiment, in order to find the best solution for each application.</p>
<p>The easiest way to render SVG files is to construct a <a href="qsvgwidget.html">QSvgWidget</a> and load an SVG file using one of the <a href="qsvgwidget.html#load">QSvgWidget::load</a>() functions. The rendering is performed by the <a href="qsvgrenderer.html">QSvgRenderer</a> class which also can be used directly to provide SVG support for custom widgets.</p>
<p>For more information, see the <a href="qtsvg.html">QtSvg</a> module documentation.</p>
</td></tr>
</table></p>
<a name="styling"></a>
<h2>Styling</h2>
<p>Qt's built-in widgets use the <a href="qstyle.html">QStyle</a> class to perform nearly all of their drawing. <a href="qstyle.html">QStyle</a> is an abstract base class that encapsulates the look and feel of a GUI, and can be used to make the widgets look exactly like the equivalent native widgets or to give the widgets a custom look.</p>
<p>Qt provides a set of <a href="qstyle.html">QStyle</a> subclasses that emulate the native look of the different platforms supported by Qt (<a href="qwindowsstyle.html">QWindowsStyle</a>, <a href="qmacstyle.html">QMacStyle</a>, <a href="qmotifstyle.html">QMotifStyle</a>, etc.)&#x2e; These styles are built into the <a href="qtgui.html">QtGui</a> library, other styles can be made available using Qt's plugin mechansim.</p>
<p>Most functions for drawing style elements take four arguments:</p>
<ul>
<li>an enum value specifying which graphical element to draw</li>
<li>a <a href="qstyleoption.html">QStyleOption</a> object specifying how and where to render that element</li>
<li>a <a href="qpainter.html">QPainter</a> object that should be used to draw the element</li>
<li>a <a href="qwidget.html">QWidget</a> object on which the drawing is performed (optional)</li>
</ul>
<p>The style gets all the information it needs to render the graphical element from the <a href="qstyleoption.html">QStyleOption</a> class. The widget is passed as the last argument in case the style needs it to perform special effects (such as animated default buttons on Mac OS X), but it isn't mandatory. In fact, <a href="qstyle.html">QStyle</a> can be used to draw on any paint device (not just widgets), in which case the widget argument is a zero pointer.</p>
<p align="center"><img src="images/paintsystem-stylepainter.png" /></p><p>The paint system also provides the <a href="qstylepainter.html">QStylePainter</a> class inheriting from <a href="qpainter.html">QPainter</a>. <a href="qstylepainter.html">QStylePainter</a> is a convenience class for drawing <a href="qstyle.html">QStyle</a> elements inside a widget, and extends <a href="qpainter.html">QPainter</a> with a set of high-level drawing functions implemented on top of <a href="qstyle.html">QStyle</a>'s API. The advantage of using <a href="qstylepainter.html">QStylePainter</a> is that the parameter lists get considerably shorter.</p>
<p><table width="100%" align="center" cellpadding="2" cellspacing="1" border="0">
<tr valign="top" class="odd"><td><img src="images/paintsystem-icon.png" /></td><td><b>QIcon</b><p>The <a href="qicon.html">QIcon</a> class provides scalable icons in different modes and states.</p>
<p><a href="qicon.html">QIcon</a> can generate pixmaps reflecting an icon's state, mode and size. These pixmaps are generated from the set of pixmaps made available to the icon, and are used by Qt widgets to show an icon representing a particular action.</p>
<p>The rendering of a <a href="qicon.html">QIcon</a> object is handled by the <a href="qiconengine.html">QIconEngine</a> class. Each icon has a corresponding icon engine that is responsible for drawing the icon with a requested size, mode and state.</p>
</td></tr>
</table></p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2008 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt 4.3.5</div></td>
</tr></table></div></address></body>
</html>
